package com.pj.lib.auraClient;


import com.pj.lib.auraClient.AuraService.LocalBinder;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

public class AuraServiceConnector extends AuraComponent{
	private AuraService mService;
	private Boolean mBound = false;
	
	public AuraServiceConnector(Aura aura, Context context) {
		super(aura);
		Intent intent = new Intent(context, AuraService.class);
		context.bindService(intent, mConnection, 0);
	}
	
	/** Defines callbacks for service binding, passed to bindService() */
    private ServiceConnection mConnection = new ServiceConnection() {        

		@Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            mService = binder.getService();
            mBound = true;
            
            getAura().fireServiceConnection(true);
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            mBound = false;
            getAura().fireServiceConnection(false);
        }
    };
    
    public AuraService getService() {
    	return mService;
    }

	public Boolean isBounded() {
		return mBound;
	}
}
